package com.github.edu.web.db.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.db.server.api.entity.TableManager;
import com.github.edu.web.db.domain.DbManagerDomain;
import com.github.edu.web.db.service.DbManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-7-31
 */
@Service
public class DbManagerServiceImpl extends BaseAbstractService implements DbManagerService {
    
    @Autowired
    private DbManagerDomain domain;

    @Override
    public JsonArray<TableManager> getAllByPage(TableManager TableManager, Pager pager) {
        String code=jsonEntity(TableManager,pager);
        JsonArray<TableManager> jsonArray=null;
        if(!StringUtils.isEmpty(code)){
            String data=domain.getALLByPager(code);
            if(!StringUtils.isEmpty(data)){
                jsonArray= JsonUtils.toCollection(data, new TypeReference<JsonArray<TableManager>>() {
                });
            }
        }
        return jsonArray;
    }

    @Override
    public TableManager getEntity(String aLong) {
        JsonEntity<TableManager> jsonEntity=JsonUtils.toCollection(domain.getEntity(aLong), new TypeReference<JsonEntity<TableManager>>(){});
        return jsonEntity.getData();
    }

    @Override
    public TableManager saveOrUpdate(TableManager TableManager) {
        JsonEntity<TableManager> jsonEntity=new JsonEntity<>();
        jsonEntity.setData(TableManager);
        String code=domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));
        JsonEntity<TableManager> json=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TableManager>>(){});
        if(null!=json){
            return json.getData();
        }
        return null;
    }

    @Override
    public String delete(String ids) {
        return domain.deleteAll(ids);
    }
}
